package cloud.seri.auth.service

import cloud.seri.auth.domain.Role
import cloud.seri.auth.repository.RoleRepository
import cloud.seri.auth.service.dto.RoleDTO
import cloud.seri.auth.service.mapper.RoleMapper
import org.slf4j.LoggerFactory

import org.springframework.stereotype.Service

import java.util.Optional

/**
 * Service Implementation for managing [Role].
 */
@Service
class RoleService(
    val roleRepository: RoleRepository,
    val roleMapper: RoleMapper
) {

    private val log = LoggerFactory.getLogger(RoleService::class.java)

    /**
     * Save a role.
     *
     * @param roleDTO the entity to save.
     * @return the persisted entity.
     */
    fun save(roleDTO: RoleDTO): RoleDTO {
        log.debug("Request to save Role : {}", roleDTO)

        var role = roleMapper.toEntity(roleDTO)
        role = roleRepository.save(role)
        return roleMapper.toDto(role)
    }

    /**
     * Get all the roles.
     *
     * @return the list of entities.
     */
    fun findAll(): MutableList<RoleDTO> {
        log.debug("Request to get all Roles")
        return roleRepository.findAll()
            .mapTo(mutableListOf(), roleMapper::toDto)
    }

    /**
     * Get one role by id.
     *
     * @param id the id of the entity.
     * @return the entity.
     */
    fun findOne(id: String): Optional<RoleDTO> {
        log.debug("Request to get Role : {}", id)
        return roleRepository.findById(id)
            .map(roleMapper::toDto)
    }

    /**
     * Delete the role by id.
     *
     * @param id the id of the entity.
     */
    fun delete(id: String) {
        log.debug("Request to delete Role : {}", id)

        roleRepository.deleteById(id)
    }
}
